// UDP Server
// 
// A single client UDP echo server
// 
// Author: F.M.A. Erich
// HAN University of Applied Science
// Arnhem, The Netherlands

using System;
using System.Collections;
using System.Net;
using System.Net.Sockets;
using System.Text;

namespace UDPServer
{
	class MainClass
	{
		public static void Main(string[] args)
		{
			if (args.Length != 1)
			{
				Console.WriteLine("This application requires the following argument: <port>");
				return;
			}

			int port = Convert.ToInt32(args[0]);
			IPEndPoint iepServer = new IPEndPoint(IPAddress.Any, port);
			Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
			
			sock.Bind(iepServer);
			
			byte[] buffer = new byte[1000];
			IPEndPoint iepClient = new IPEndPoint(IPAddress.Any, 0);
			EndPoint epClient = (EndPoint)iepClient;
			
			while (true)
			{
				sock.ReceiveFrom(buffer, ref epClient);
				Console.WriteLine("{0}", Convert.ToString(buffer));
				sock.SendTo(buffer, epClient);
			}
		}
	}
}